﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

namespace 凌依.工具库
{
    //ScriptableObject类使用规范
    /*
    ***********************************创建储存类*****************************************

     [CreateAssetMenu(fileName = "新道具信息", menuName = "创建道具/道具")]
     public class 道具 : ScriptableObject
     {
        public int 数量;
        public 详情 详情;
     }



    *************************************创建公开变量储存类**************************************************

    [System.Serializable]//公开类变量  可以被储存不添加此代码将无法储存类变量
    public class 详情 
    {
         public string 描述;
         public Color 色彩;
    }

      */
    public class 序列化工具_U3D 
    {
        public static string 序列化_U3D_Json(object 对象)
        {
            return 序列化工具.格式化_json(JsonUtility.ToJson(对象));
        }
        public static string 序列化_U3D_Json字典<TKey,TValue>(Dictionary<TKey, TValue> obj)
        {
            return 序列化工具.格式化_json(JsonUtility.ToJson(new 序列化字典<TKey,TValue>(obj)));
        }

        public static Dictionary<TKey, TValue> 反序列化_U3D_Json字典<TKey, TValue>(string json)
        {
            Dictionary<TKey, TValue> fanhui = JsonUtility.FromJson<序列化字典<TKey, TValue>>(json).ToDictionary();
            return fanhui;
        }

        public static T 反序列化_U3D_Json<T>(string json) where T : class,new()
        {
            T a = new T();
            JsonUtility.FromJsonOverwrite(json, a);
            return a;
        }
        public static T 克隆<T>(T 对象) where T : class,new()
        {
            string json = 序列化_U3D_Json(对象);
            return 反序列化_U3D_Json<T>(json);
        }


    }
    public class U3D文件操作
    {
        /// <summary>
        /// 复制类
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T 克隆<T>(T 对象) where T : class,new()
        {
            string json =  序列化工具_U3D.序列化_U3D_Json(对象);
            return 序列化工具_U3D.反序列化_U3D_Json<T>(json);
        }
        /// <summary>
        /// 返回unity保存文件地址
        /// </summary>
        /// <returns>绝对路径</returns>
        public static string 获取_保存地址()
        {
            return Application.persistentDataPath;
        }

        public static string 获取_工作路径()
        {
            return Application.dataPath;
        }

    }
    [System.Serializable]
    public class 序列化字典<TKey, TValue> : ISerializationCallbackReceiver
    {
        [SerializeField]
        List<TKey> keys;
        [SerializeField]
        List<TValue> values;

        Dictionary<TKey, TValue> target;
        public Dictionary<TKey, TValue> ToDictionary() { return target; }

        public 序列化字典(Dictionary<TKey, TValue> target)
        {
            this.target = target;
        }

        public void OnBeforeSerialize()
        {
            keys = new List<TKey>(target.Keys);
            values = new List<TValue>(target.Values);
        }

        public void OnAfterDeserialize()
        {
            var count = Mathf.Min(keys.Count, values.Count);
            target = new Dictionary<TKey, TValue>(count);
            for (var i = 0; i < count; ++i)
            {
                target.Add(keys[i], values[i]);
            }
        }
    }

}